package com.oplink.webems.modules.alarm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.modules.alarm.dao.AlarmPropDao;
import com.oplink.webems.modules.alarm.dao.AlarmSeverDao;
import com.oplink.webems.modules.alarm.dao.AlarmTypeDao;
import com.oplink.webems.modules.alarm.entity.AlarmHistory;
import com.oplink.webems.modules.alarm.entity.AlarmProp;
import com.oplink.webems.modules.alarm.entity.AlarmSeverity;
import com.oplink.webems.modules.alarm.entity.AlarmType;
import com.oplink.webems.modules.cfg.dao.CfgCardTypeDao;
import com.oplink.webems.modules.cfg.dao.CfgNeDao;
import com.oplink.webems.modules.cfg.dao.CfgNeTypeDao;
import com.oplink.webems.modules.cfg.dao.CfgSiteDao;
import com.oplink.webems.modules.cfg.dao.CfgSlotDao;
import com.oplink.webems.modules.cfg.entity.CfgCardType;
import com.oplink.webems.modules.cfg.entity.CfgNe;
import com.oplink.webems.modules.cfg.entity.CfgNeType;
import com.oplink.webems.modules.cfg.entity.CfgSite;
import com.oplink.webems.modules.cfg.entity.CfgSlot;

/**
 * Alarm Properties Service
 * @author yizhang
 */
@Component
@Transactional
public class AlarmPropService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(AlarmPropService.class);
	
	@Autowired
	private AlarmPropDao alarmpropDao;
	
	@Autowired
	private AlarmTypeDao atypeDao;
	
	@Autowired
	private AlarmSeverDao asevDao;
	
	@Autowired
	private CfgCardTypeDao ctypeDao;
	
	public Page<AlarmProp> find(Page<AlarmProp> page, AlarmProp alarmprop) {
		DetachedCriteria dc = alarmpropDao.createDetachedCriteria();
		
		if ((alarmprop.getAname() != null) && StringUtils.isNotEmpty(alarmprop.getAname())) {
		  dc.add(Restrictions.like("aname", "%" + alarmprop.getAname() + "%"));		
		}
		
		if ((alarmprop.getCardtype() != null) && alarmprop.getCardtype().getId()!=0) {
		  dc.add(Restrictions.eq("cardtype.id", alarmprop.getCardtype().getId()));		
		}
		
//		if ((alarmprop.getNenode() != null) && StringUtils.isNotEmpty(alarmprop.getNenode().getIp())) {
//		  if (!alarmprop.getNenode().getIp().equals("0")){
//			  DetachedCriteria nenodedc = neDao.createDetachedCriteria();
//			  dc.createCriteria("nenode").add(Restrictions.eq("ip", alarmprop.getNenode().getIp())); 			  
//		  }		
//		}		
		
//		if (alarmprop.getSlot() != 0) {
//			System.out.println("2222 >>"+alarmprop.getSlot());
//			  dc.add(Restrictions.eq("slot", new Integer(alarmprop.getSlot())));		
//		}			
		
		if ((alarmprop.getSeverity() != null) && alarmprop.getSeverity().getId()!=0) {
			  dc.add(Restrictions.eq("severity.id", alarmprop.getSeverity().getId()));		
		}		
		
		if ((alarmprop.getAlarmtype() != null) && alarmprop.getAlarmtype().getAid()!=0) {
			  dc.add(Restrictions.eq("alarmtype.aid", alarmprop.getAlarmtype().getAid()));		
		}		
		
		if (alarmprop.getFilter() != 2) {
			  dc.add(Restrictions.eq("filter", new Integer(alarmprop.getFilter())));		
		}		
//		dc.addOrder(Order.desc("sort"));
		return alarmpropDao.find(page, dc);
	}
	
	public AlarmProp getAlarmprop(Long id) {
		AlarmProp aprop = alarmpropDao.findOne(id);
		return aprop;
	}
	
	public void saveAlarmProp(AlarmProp alarmprop) {
		alarmpropDao.save(alarmprop);
	}	
	
	public AlarmProp getAlarmPropData(String frmData){
		JSONObject aobj = JSON.parseObject(frmData);
		AlarmProp aprop = alarmpropDao.findOne(Long.parseLong(aobj.getString("m_id")));
		//aprop.getSeverity().setId(Long.parseLong(aobj.getString("m_severity")));
		AlarmSeverity as = asevDao.findOne(Long.parseLong(aobj.getString("m_severity")));
		aprop.setSeverity(as);
        //aprop.getAlarmtype().setAid(Long.parseLong(aobj.getString("m_alarmtype")));
		AlarmType at = atypeDao.findOne(Long.parseLong(aobj.getString("m_alarmtype")));
		aprop.setAlarmtype(at);
		aprop.setFilter(Integer.parseInt(aobj.getString("m_filter")));
		aprop.setAname(aobj.getString("m_aname"));
		if (aobj.getString("m_cardtype")!=null){
		if (!aobj.getString("m_cardtype").equals("0")){
			CfgCardType ctype = ctypeDao.findOne(Long.parseLong(aobj.getString("m_cardtype")));
			aprop.setCardtype(ctype);
		    //aprop.getCardtype().setId(Long.parseLong(aobj.getString("m_cardtype")));
		}
		}
		aprop.setDescription(aobj.getString("m_description"));
		return aprop;
	}
	
	public List<AlarmType> getAllAlarmType() {
		return (List<AlarmType>)atypeDao.findAll();
	}		
	
	@SuppressWarnings("rawtypes")
	public String getAlarmPropJson(AlarmProp aprop){
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		map.put("m_aname", aprop.getAname()==null?"":aprop.getAname());
		map.put("m_alarmtype", aprop.getAlarmtype()==null?"1":aprop.getAlarmtype().getAid());
		map.put("m_cardtype", aprop.getCardtype()==null?"1":aprop.getCardtype().getId());
		map.put("m_severity", aprop.getSeverity()==null?"1":aprop.getSeverity().getId());
		map.put("m_filter", aprop.getFilter());
		map.put("m_id", aprop.getId());
		map.put("m_description", aprop.getDescription()==null?"":aprop.getDescription());
		String json = JSONObject.toJSONString(map);
		return json;
	}
	
	@SuppressWarnings("rawtypes")
	public List<Map> getCardInfo(List<CfgSlot> slist){
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		List<Map> list = new ArrayList<Map>();
		  for (Iterator<CfgSlot> it = slist.iterator(); it.hasNext();) {
			  CfgSlot obj = (CfgSlot) it.next();
			  map = new HashMap<String, Comparable>();
			  map.put("cardid", obj.getCard().getCardid());
			  map.put("name", Integer.toString(obj.getNum()).concat(":").concat(obj.getCard().getCardtype().getCardtype()));
			  list.add(map);
		  }
		return list;
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
